package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupAndCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupAndCheckItem;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 *
 */
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupAndCheckItemMapper checkGroupAndCheckItemMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {

        Page<CheckGroup> checkGroups;

        //生成where
        Example example = new Example(CheckGroup.class);
        Example.Criteria criteria = example.createCriteria();

        if (queryString!=null&&queryString!=""){
            //where 语句
            criteria.orLike("code","%"+queryString+"%")
                    .orLike("name","%"+queryString+"%")
                    .orLike("helpCode","%"+queryString+"%");

            PageHelper.startPage(currentPage,pageSize);
            checkGroups = (Page<CheckGroup>) checkGroupMapper.selectByExample(example);
        }else{
            //写入到线程中，自动注入
            PageHelper.startPage(currentPage,pageSize);
            checkGroups = (Page<CheckGroup>) checkGroupMapper.selectAll();
        }

        return new PageResult(checkGroups.getTotal(),checkGroups.getResult());
    }

    /**
     * 根据检查组查询检查项
     * @param id
     * @return
     */
    @Transactional
    @Override
    public List<CheckItem> findCheckItemById(Integer id) {
        Example example = new Example(CheckGroupAndCheckItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("checkGroupId",id);

        List<CheckGroupAndCheckItem> checkGroupAndCheckItems = checkGroupAndCheckItemMapper.selectByExample(example);
        ArrayList<Integer> ids = new ArrayList<>();
        for (CheckGroupAndCheckItem groupAndCheckItem : checkGroupAndCheckItems) {
            ids.add(groupAndCheckItem.getCheckItemId());
        }

        ArrayList<CheckItem> checkItems = new ArrayList<>();
        for (Integer checkItemId : ids) {
            CheckItem checkItem = checkItemMapper.selectByPrimaryKey(checkItemId);
            checkItems.add(checkItem);
        }

        return checkItems;
    }

    /**
     * 获取单条
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param checkGroup
     * @param ids
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] ids) {
        //插入数据
        checkGroupMapper.insert(checkGroup);

        //根据checkGroup查找
        CheckGroup checkGroup2 = checkGroupMapper.selectOne(checkGroup);
        Integer checkGroupId = checkGroup2.getId();

        for (Integer id : ids) {
            CheckGroupAndCheckItem checkGroupAndCheckItem = new CheckGroupAndCheckItem(checkGroupId, id);
            checkGroupAndCheckItemMapper.insert(checkGroupAndCheckItem);
        }
    }

    /**
     * 更新
     * @param checkGroup
     * @param ids
     */
    @Transactional
    @Override
    public void update(CheckGroup checkGroup, Integer[] ids) {
        checkGroupMapper.updateByPrimaryKey(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        //根据检查组id查询其下的所有检查项id
        if(ids.length<=0)  return;
        Example example = new Example(CheckGroupAndCheckItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("checkGroupId",checkGroupId);
        criteria.andNotIn("checkItemId", Arrays.asList(ids));
        List<CheckGroupAndCheckItem> checkGroupAndCheckItems = checkGroupAndCheckItemMapper.selectByExample(example);
        for (CheckGroupAndCheckItem checkGroupAndCheckItem : checkGroupAndCheckItems) {
            checkGroupAndCheckItemMapper.delete(checkGroupAndCheckItem);
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        checkGroupMapper.deleteByPrimaryKey(id);
    }

    /**
     * 获取全部
     * 原生mybatis和通用mapper只能存在一个
     * mybatisPlus也不能一起
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.selectAll();
    }
}
